package com.fs.web.yz.controller;

import com.alibaba.fastjson.JSON;
import com.fs.common.pojo.WechatPayConfig;
import com.fs.common.utils.ConcurrentHashMapCacheUtils;
import com.fs.common.yzMapper.WechatPayConfigMapper;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.vo.JsonResponse;
import com.fs.web.base.vo.SysPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DeveloperKK
 * @desc微信支付设置
 * @creationTime 2019-08-22 09:12
 * @projectName yuezi-menu
 * @packageName com.fs.web.yz.controller
 */
@Slf4j
@Controller
@RequestMapping("/console/wechatPay/rewrite")
public class PayConfigController {

    @Autowired
    private WechatPayConfigMapper wechatPayConfigMapper;

    @RequestMapping("/config")
    public String index(HttpServletRequest request){
        WechatPayConfig config = wechatPayConfigMapper.selectById(1);
        request.setAttribute("infoData", JSON.toJSONString(config));
        return SysPath.BACK_PATH.concat("/wechatPayConfig/").concat("info");
    }

    @ResponseBody
    @RequestMapping("/modify")
    public JsonResponse modify(HttpServletRequest request) {
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            WechatPayConfig old = wechatPayConfigMapper.selectById(1);
            WechatPayConfig newBean = JSON.parseObject(JSON.toJSONString(map),WechatPayConfig.class);
            BeanUtils.copyProperties(old,newBean,"id");
            wechatPayConfigMapper.updateById(newBean);
            ConcurrentHashMapCacheUtils.deleteCache(WechatPayConfig.CACHE_KEY);
            ConcurrentHashMapCacheUtils.setCache(WechatPayConfig.CACHE_KEY,newBean);
            return JsonResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

}
